package io.zcoder.innoway.activities;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.models.Profile;
import io.zcoder.innoway.models.User;
import io.zcoder.innoway.widgets.ScrollSelectDateView;
import io.zcoder.innoway.widgets.TextViewCustom;

public class SelectBirthDayActivity extends BaseActivity implements ScrollSelectDateView.SelectCallBack {

    public static final int RESULT_SET_BIRTHDAY = 34;
    @BindView(R.id.date_year)
    ScrollSelectDateView mDateYear;
    @BindView(R.id.date_month)
    ScrollSelectDateView mDateMonth;
    @BindView(R.id.date_day)
    ScrollSelectDateView mDateDay;
    @BindView(R.id.next)
    TextViewCustom mNext;

    @OnClick(R.id.next)
    void onNext() {

        String birthday = mDateYear.getValue() + "-" + (mDateMonth.getIndex() + 1) + "-" + mDateDay.getValue();
        if (mFromEdit) {
            Intent intent = new Intent();
            intent.putExtra(Constants.BIRTHDAY_EXTRAS, birthday);
            intent.putExtra(Constants.AGE_EXTRAS, Calendar.getInstance().get(Calendar.YEAR) - Integer.valueOf(mDateYear.getValue()));
            setResult(RESULT_SET_BIRTHDAY, intent);
            finish();
            return;
        }
        if (mProfile == null) {
            return;
        }
        Intent intent = new Intent(this, BasicInformationActivity.class);
        mProfile.setBirthday(birthday);
        intent.putExtra(Constants.PROFILE_EXTRAS, mProfile);
        startActivity(intent);
    }

    private Profile mProfile;
    private boolean mFromEdit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_select_birthday_activity);
        ButterKnife.bind(this);
        showBack(true);
        mProfile = getIntent().getParcelableExtra(Constants.PROFILE_EXTRAS);
        mFromEdit = getIntent().getBooleanExtra(Constants.EDIT_INFO_EXTRAS, false);
        mDateMonth.setCallBack(this);
        mDateYear.setCallBack(this);
        if (mFromEdit) {
            mNext.setText(getString(R.string.save));
            String birthday = getIntent().getStringExtra(Constants.BIRTHDAY_EXTRAS);
            if (birthday != null) {
                if (birthday.contains("T")) {
                    birthday = birthday.substring(0, birthday.indexOf("T"));
                }
                String[] birthdaySplit = birthday.split("-");
                if (birthdaySplit.length == 3) {
                    mDateYear.setValue(Integer.valueOf(birthdaySplit[0]));
                    mDateMonth.setValue(Integer.valueOf(birthdaySplit[1]));
                    mDateDay.setDayOfMonth(Integer.valueOf(birthdaySplit[2]),
                            Integer.valueOf(birthdaySplit[1]), Integer.valueOf(birthdaySplit[0]));
                }
            }
        }

    }

    @Override
    public void onSelected() {
        mDateDay.postDelayed(new Runnable() {
            @Override
            public void run() {
                mDateDay.setDayOfMonth(mDateMonth.getIndex(), Integer.valueOf(mDateYear.getValue()));
            }
        }, 500);
    }
}
